﻿using JobBack.Models;
using QG.JobBack.BLL.Service;
using QG.JobBack.DAL;
using QH.JobBack.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace JobBack.Controllers
{
    public class ClientController : Controller
    {
        UserinfoService userinfoService = new UserinfoService();
        CompanyService companyService = new CompanyService();
        Opretation opretation = new Opretation();
        /// <summary>
        /// 求职者管理
        /// </summary>
        /// <returns></returns>
        public ActionResult UserManager()
        {
            return View();
        }
        /// <summary>
        /// 企业管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult FirmManager()
        {
            return View();
        }
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public JsonResult QueryUserList(int page, int limit,string UserName)
        {
            if (UserName == null)
            {
                UserName = "";
            }           
            int count = 0;
            List<UserInfo> menus = userinfoService.QueryPageList(page, limit, a=>a.UserName.Contains(UserName), a => a.UserId);
            ResponseResult<UserInfo> getJson = new ResponseResult<UserInfo>()
            {
                msg = "",
                code = 0,
                data = menus,
                count = userinfoService.GetCount(a => true, out count)
            };
            return Json(getJson, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 修改用户状态
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public JsonResult UpdateUserStar(UserInfo userInfo)
        {
            opretation.Success = userinfoService.UpdateStatus(userInfo);
            return Json(opretation);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public JsonResult DeleteUsers(UserInfo userInfo)
        {
            opretation.Success = userinfoService.Delete(userInfo);
            return Json(opretation);
        }

        /// <summary>
        /// 黑名单
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="BlackType">0为用户黑名单，1为公司黑名单</param>
        /// <returns></returns>
        public JsonResult BlacklistBaseShow(int page, int limit, string Types)
        {
            List<BlacklistBase> Black = null;
            Nullable<bool> BlackType = Convert.ToBoolean(Types);
            using (var db = new qgpingEntities())
            {
                var str = from a in db.Blacklist
                          join b in db.UserInfo
                          on a.UserId equals b.UserId
                          join c in db.Company
                          on a.CompId equals c.CompId
                          where a.BlackType == BlackType
                          select new BlacklistBase()
                          {
                              Id = a.Id,
                              UserName = b.UserName,
                              ComFullName = c.ComFullName

                          };
                Black = str.ToList<BlacklistBase>();
            }
            ResponseResult<BlacklistBase> getJson = new ResponseResult<BlacklistBase>()
            {
                msg = "",
                code = 0,
                count = Black.Count,
                data = Black.Skip((page - 1) * limit).Take(limit).ToList()
            };
            return Json(getJson, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取公司列表
        /// </summary>
        /// <returns></returns>
        public JsonResult QueryCompanyList(int page, int limit,string ComFullName)
        {
            List<CompanyBase> Black = null;
            using (var db = new qgpingEntities())
            {
                
                if (!string.IsNullOrEmpty(ComFullName))
                {
                    var str = from a in db.Company
                              join b in db.Cityinfo
                              on a.CityId equals b.CityId
                              join c in db.Industry
                              on a.IndustryId equals c.IndustryId
                              join d in db.DevelopmentPhase
                              on a.PhaseId equals d.PhaseId
                              select new CompanyBase()
                              {
                                  CompId = a.CompId,
                                  ComFullName = a.ComFullName,
                                  ComForShort = a.ComFullName,
                                  CompanyLoGo = a.CompanyLoGo,
                                  ComWebsite = a.ComWebsite,
                                  CityId = b.CityId,
                                  IndustryId = c.IndustryId,
                                  ComScale = a.ComScale,
                                  PhaseId = d.PhaseId,
                                  OneRecommend = a.OneRecommend,
                                  CompanyPhone = a.CompanyPhone,
                                  CompanyEmail = a.CompanyEmail,
                                  CompanyRemark = a.CompanyRemark,
                                  CityName = b.CityName,
                                  IndustryName = c.IndustryName,
                                  PhaseName = d.PhaseName

                              };
                    Black = str.Where(a=>a.ComFullName.Contains(ComFullName)).ToList<CompanyBase>();
                }
                else
                {
                    var str = from a in db.Company
                              join b in db.Cityinfo
                              on a.CityId equals b.CityId
                              join c in db.Industry
                              on a.IndustryId equals c.IndustryId
                              join d in db.DevelopmentPhase
                              on a.PhaseId equals d.PhaseId
                              select new CompanyBase()
                              {
                                  CompId = a.CompId,
                                  ComFullName = a.ComFullName,
                                  ComForShort = a.ComFullName,
                                  CompanyLoGo = a.CompanyLoGo,
                                  ComWebsite = a.ComWebsite,
                                  CityId = b.CityId,
                                  IndustryId = c.IndustryId,
                                  ComScale = a.ComScale,
                                  PhaseId = d.PhaseId,
                                  OneRecommend = a.OneRecommend,
                                  CompanyPhone = a.CompanyPhone,
                                  CompanyEmail = a.CompanyEmail,
                                  CompanyRemark = a.CompanyRemark,
                                  CityName = b.CityName,
                                  IndustryName = c.IndustryName,
                                  PhaseName = d.PhaseName

                              };
                    Black = str.ToList<CompanyBase>();
                }  
            }
            ResponseResult<CompanyBase> getJson = new ResponseResult<CompanyBase>()
            {
                msg = "",
                code = 0,
                count = Black.Count,
                data = Black.Skip((page - 1) * limit).Take(limit).ToList()
            };
            return Json(getJson, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除公司信息
        /// </summary>
        /// <param name="company"></param>
        /// <returns></returns>
        public JsonResult DeleteCompany(Company company)
        {
            opretation.Success = companyService.Delete(company);
            return Json(opretation);
        }

    }
}